package com.deyuanyun.pic.pbf.controller;

import com.alibaba.fastjson.JSON;
import com.deyuanyun.pic.common.util.MapUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pbf.domain.*;
import com.deyuanyun.pic.pbf.enumeration.RepairStateEnum;
import com.deyuanyun.pic.pbf.service.ExternalRepairService;
import com.deyuanyun.pic.pbf.service.PipeBodyDefectService;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.pipe.domain.CenterLine;
import com.deyuanyun.pic.pipe.service.PipeCenterLineService;
import com.deyuanyun.pic.settings.domain.base.Attachment;
import com.deyuanyun.pic.settings.domain.util.DomainCodeInterceptor;
import com.deyuanyun.pic.settings.service.base.AttachmentService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.deyuanyun.pic.wf.domain.ProcessDefinition;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 非内检测缺陷修复控制层
 *
 * @author lijun
 * @date 2016-06-25
 * <p/>
 * History: [修改人][时间][修改描述]
 */

@Controller
@RequestMapping("/pbf/externalRepair")
public class ExternalRepairController extends BaseController {


    @Resource
    private ExternalRepairService externalRepairService;

    @Resource
    private PipeCenterLineService pipeCenterLineService;

    @Resource
    private AttachmentService attachmentService;

    //修复页面初始化
    @RequestMapping("/repairCoatInit.html")
    public ModelAndView repairCoatInit(String defectId,ModelMap model) {

        model.addAttribute("defectId",defectId);
        return forword("/pbf/noninternal/noninternalRepairCoat",model);
    }

    @RequestMapping("/repairCompositesInit.html")
    public ModelAndView repairCompositesInit(String defectId,ModelMap model) {

        model.addAttribute("defectId",defectId);
        return forword("/pbf/noninternal/noninternalRepairComposites",model);
    }

    @RequestMapping("/repairEpoxySleeveInit.html")
    public ModelAndView repairEpoxySleeveInit(String defectId,ModelMap model) {

        model.addAttribute("defectId",defectId);
        return forword("/pbf/noninternal/noninternalRepairEpoxySleeve",model);
    }

    @RequestMapping("/repairABTypeSleeveInit.html")
    public ModelAndView repairABTypeSleeveInit(String defectId,ModelMap model) {
        ProcessDefinition definition = externalRepairService.queryRepairNameByDefectId(defectId);
        model.addAttribute("defectId",defectId);
        model.addAttribute("definition",definition);
        return forword("/pbf/noninternal/noninternalRepairABTypeSleeve",model);
    }

    @RequestMapping("/repairReplacePipeInit.html")
    public ModelAndView repairReplacePipeInit(String defectId,ModelMap model) {
        CenterLine centerLine = pipeCenterLineService.getCenterLineByDefectId(defectId);
        model.addAttribute("defectId",defectId);
        if(centerLine != null ){
            model.addAttribute("gisId",centerLine.getGisId());
        }

        return forword("/pbf/noninternal/noninternalRepairReplacePipe",model);
    }

    @RequestMapping("/repairOtherInit.html")
    public ModelAndView repairOtherInit(String defectId,ModelMap model) {
        ProcessDefinition definition = externalRepairService.queryRepairNameByDefectId(defectId);
        model.addAttribute("defectId",defectId);
        model.addAttribute("definition",definition);
        return forword("/pbf/noninternal/noninternalRepairOther",model);
    }

    /**
     * 通过材质A ID 得到材质B
     *
     * @param parentId 所选材质A ID
     */
    @ResponseBody
    @RequestMapping("/getMaterialB.json")
    public void getMaterialB(String parentId) {
        if (parentId != null && !"".equals(parentId)) {
            AjaxSupport.sendSuccessText("success", CodeUtil.getCodeListByParentId("MATERIAL", parentId));
        } else {
            AjaxSupport.sendFailText("fail", "");
        }
    }


    //创建修复记录

    @ResponseBody
    @RequestMapping("/repairCoatCreate.json")
    public void repairCoatCreate(CoatRepair coat,String defectId,
                                 @RequestParam(value = "att",required = false)String att[]) {
        int num = externalRepairService.repairCoatCreate(coat,defectId,att);

        if(num>0){
            AjaxSupport.sendSuccessText("success","");
        }else{
            AjaxSupport.sendFailText("fail","");
        }

    }

    @ResponseBody
    @RequestMapping("/repairCompositesCreate.json")
    public void repairCompositesCreate(RepairComposites composites,String defectId,
                                       @RequestParam(value = "att",required = false)String att[]) {
        int num = externalRepairService.repairCompositesCreate(composites,defectId,att);

        if(num>0){
            AjaxSupport.sendSuccessText("success","");
        }else{
            AjaxSupport.sendFailText("fail","");
        }
    }

    @RequestMapping("/repairEpoxySleeveCreate.json")
    public void repairEpoxySleeveCreate(RepairEpoxySleeve epoxySleeve,String defectId,
                                        @RequestParam(value = "att",required = false)String att[]) {
        int num = externalRepairService.repairEpoxySleeveCreate(epoxySleeve,defectId,att);

        if(num>0){
            AjaxSupport.sendSuccessText("success","");
        }else{
            AjaxSupport.sendFailText("fail","");
        }
    }

    @ResponseBody
    @RequestMapping("/repairABTypeSleeveCreate.json")
    public void repairABTypeSleeveCreate(RepairABTypeSleeve ABTypeSleeve,String defectId,
                                         @RequestParam(value = "att",required = false)String att[]) {
        int num = externalRepairService.repairABTypeSleeveCreate(ABTypeSleeve,defectId,att);

        if(num>0){
            AjaxSupport.sendSuccessText("success","");
        }else{
            AjaxSupport.sendFailText("fail","");
        }
    }

    @RequestMapping("/repairReplacePipeCreate.json")
    public void repairReplacePipeCreate(RepairReplacePipe replacePipe,String defectId,
                                        @RequestParam(value = "imgsReplace",required = false)String imgsReplace[],
                                        @RequestParam("gwwNumbers")String[] gwwNumbers,@RequestParam("x")String[] x,
                                        @RequestParam("y")String[] y,@RequestParam("z")String[] z,
                                        RepairPipeWelding repairPipeWelding,String weldingRemark,
                                        @RequestParam(value = "imgsWelding",required = false)String imgsWelding[],
                                        ModelMap model) {
        //焊缝集合
        List<RepairPipeWeld> repairPipeWeldList = new ArrayList<RepairPipeWeld>();
        for (int i = 0; i < gwwNumbers.length ; i++){
            RepairPipeWeld repairPipeWeld =new RepairPipeWeld();
            repairPipeWeld.setGwwNumber(gwwNumbers[i]);
            repairPipeWeld.setX(new BigDecimal(x[i]));
            repairPipeWeld.setY(new BigDecimal(y[i]));
            repairPipeWeld.setZ(new BigDecimal(z[i]));
            repairPipeWeldList.add(repairPipeWeld);
        }
        //为换管焊接记录写入新建备注
        if(weldingRemark != null){
            repairPipeWelding.setCreateRemark(weldingRemark);
        }
        //添加换管记录
        int successNum = externalRepairService.repairReplacePipeCreate(replacePipe,defectId,imgsReplace,repairPipeWelding,imgsWelding,repairPipeWeldList);
        if(successNum > 0){
            model.addAttribute("successNum",successNum);
            AjaxSupport.sendSuccessText("success",model);
        }else{
            AjaxSupport.sendFailText("fail","");
        }


    }


    @ResponseBody
    @RequestMapping("/repairOtherCreate.json")
    public void repairOtherCreate(RepairOther other,String defectId,
                                  @RequestParam(value = "att",required = false)String att[]) {
        int num = externalRepairService.repairOtherCreate(other,defectId,att);

        if(num>0){
            AjaxSupport.sendSuccessText("success","");
        }else{
            AjaxSupport.sendFailText("fail","");
        }
    }

    //查看修复记录

    @ResponseBody
    @RequestMapping("/repairCoatShow.json")
    public void repairCoatShow(String defectId)  {
        if (defectId != null && !"".equals(defectId)) {

            Map<String, Object> coat = new HashMap<String, Object>();

            CoatRepair repairCoat = externalRepairService.queryRepairCoatByDefectId(defectId);
            if (repairCoat != null) {
                repairCoat = DomainCodeInterceptor.newProxyInstance(repairCoat);
                //附件
                List<Attachment> att = attachmentService.queryAttachment(repairCoat.getId());

                Map<String, Object> map =(Map<String, Object>) JSON.toJSON(repairCoat);
                map.put("att", att);
                coat.put("repairCoat", map);
                AjaxSupport.sendSuccessText("success", coat);
            } else {
                AjaxSupport.sendFailText("no", "");
            }
        } else {
            AjaxSupport.sendFailText("fail", "");
        }
    }

    @ResponseBody
    @RequestMapping("/repairCompositesShow.json")
    public void repairCompositesShow(String defectId) {
        if (defectId != null && !"".equals(defectId)) {

            Map<String, Object> composites = new HashMap<String, Object>();

            RepairComposites repairComposites = externalRepairService.queryRepairCompositesByDefectId(defectId);
            if (repairComposites != null) {
                repairComposites = DomainCodeInterceptor.newProxyInstance(repairComposites);
                //附件
                List<Attachment> att = externalRepairService.queryAttachmentOrderByCustom(repairComposites.getId());
                Map<String, Object> map =(Map<String, Object>) JSON.toJSON(repairComposites);
                map.put("att", att);
                composites.put("repairComposites", map);


                AjaxSupport.sendSuccessText("success", composites);
            } else {
                AjaxSupport.sendFailText("no", "");
            }
        } else {
            AjaxSupport.sendFailText("fail", "");
        }
    }

    @ResponseBody
    @RequestMapping("/repairEpoxySleeveShow.json")
    public void repairEpoxySleeveShow(String defectId) {
        if (defectId != null && !"".equals(defectId)) {

            Map<String, Object> epoxySleeve = new HashMap<String, Object>();

            RepairEpoxySleeve repairEpoxySleeve = externalRepairService.queryRepairEpoxySleeveByDefectId(defectId);

            if (repairEpoxySleeve != null) {
                repairEpoxySleeve = DomainCodeInterceptor.newProxyInstance(repairEpoxySleeve);
                //附件
                List<Attachment> att = externalRepairService.queryAttachmentOrderByCustom(repairEpoxySleeve.getId());
                Map<String, Object> map =(Map<String, Object>) JSON.toJSON(repairEpoxySleeve);
                map.put("att", att);
                epoxySleeve.put("repairEpoxySleeve", map);

                AjaxSupport.sendSuccessText("success", epoxySleeve);
            } else {
                AjaxSupport.sendFailText("no", "");
            }

        } else {
            AjaxSupport.sendFailText("fail", "");
        }
    }

    @ResponseBody
    @RequestMapping("/repairABTypeSleeveShow.json")
    public void repairABTypeSleeveShow(String defectId){
        if (defectId != null && !"".equals(defectId)) {

            Map<String, Object> aBTypeSleeve = new HashMap<String, Object>();

            RepairABTypeSleeve repairABTypeSleeve = externalRepairService.queryRepairABTypeSleeveByDefectId(defectId);

            if (repairABTypeSleeve != null) {
                repairABTypeSleeve = DomainCodeInterceptor.newProxyInstance(repairABTypeSleeve);
                //附件
                List<Attachment> att = externalRepairService.queryAttachmentOrderByCustom(repairABTypeSleeve.getId());
                Map<String, Object> map = (Map<String, Object>) JSON.toJSON(repairABTypeSleeve);
                map.put("att", att);
                aBTypeSleeve.put("repairABTypeSleeve", map);
                AjaxSupport.sendSuccessText("success", aBTypeSleeve);
            } else {
                AjaxSupport.sendFailText("no", "");
            }
        } else {
            AjaxSupport.sendFailText("fail", "");
        }
    }

    @ResponseBody
    @RequestMapping("/repairReplacePipeShow.json")
    public void repairReplacePipeShow(String defectId){

        Map<String, Object> replacePipe = new HashMap<String, Object>();
        if (defectId != null && !"".equals(defectId)) {

            //得到换管信息
            RepairReplacePipe repairReplacePipe = externalRepairService.queryRepairReplacePipeByDefectId(defectId);
            if (repairReplacePipe != null) {
                repairReplacePipe = DomainCodeInterceptor.newProxyInstance(repairReplacePipe);
                //附件
                List<Attachment> imgsReplace = externalRepairService.queryAttachmentOrderByCustom(repairReplacePipe.getId());
                Map<String, Object> map =(Map<String, Object>) JSON.toJSON(repairReplacePipe);
                map.put("imgsReplace", imgsReplace);

                replacePipe.put("repairReplacePipe", map);
                //得到焊接信息
                RepairPipeWelding repairPipeWelding = externalRepairService.queryRepairPipeWeldingByReplacePipeId(repairReplacePipe.getId());
                if (repairPipeWelding != null) {
                    repairPipeWelding = DomainCodeInterceptor.newProxyInstance(repairPipeWelding);
                    if(StringUtils.isNotNullEmpty(repairPipeWelding.getRepairind()) ){
                        repairPipeWelding.setRepairind( "0".equals(repairPipeWelding.getRepairind())?"否":"是");
                    }
                    Map<String, Object> mapWeld =(Map<String, Object>) JSON.toJSON(repairPipeWelding);
                    //附件
                    List<Attachment> imgsWelding = attachmentService.queryAttachment(repairPipeWelding.getId());
                    mapWeld.put("imgsWelding", imgsWelding);
                    //得到焊缝信息
                    List<RepairPipeWeld> repairPipeWeld = externalRepairService.queryRepairPipeWeldByWeldingId(repairPipeWelding.getId());
                    repairPipeWeld = DomainCodeInterceptor.newProxyInstance(repairPipeWeld);
                    mapWeld.put("repairPipeWeld", repairPipeWeld);
                    replacePipe.put("repairPipeWelding", mapWeld);
                }

                AjaxSupport.sendSuccessText("success",StringUtils.DATE, replacePipe);
            } else {
                AjaxSupport.sendFailText("no", "");
            }
        } else {
            AjaxSupport.sendFailText("fail", "");
        }

    }

    @ResponseBody
    @RequestMapping("/repairOtherShow.json")
    public void repairOtherShow(String defectId) {
        if (defectId != null && !"".equals(defectId)) {
            RepairOtherVO repairOther = externalRepairService.queryRepairOtherByDefectId(defectId);

            if (repairOther != null) {

                Map<String, Object> other = new HashMap<String, Object>();
                //附件
                List<Attachment> att = externalRepairService.queryAttachmentOrderByCustom(repairOther.getId());
                Map<String, Object> map =(Map<String, Object>) JSON.toJSON(repairOther);
                map.put("att",att);

                other.put("repairOther", map);

                AjaxSupport.sendSuccessText("success", other);
            } else {
                AjaxSupport.sendFailText("no", "");
            }
        } else {
            AjaxSupport.sendFailText("fail", "");
        }

    }
}
